/*
 * Copyright (c) 2009, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_TRECT_H_INCLUDED_
#define ALKES_TRECT_H_INCLUDED_

#include "alkes/internal.h"

namespace alkes {

#define initTRect(left, top, right, bottom) {left, top, (right)-(left), (bottom)-(top)}

template<typename T>
struct TRectSize {
    typedef T value_type;

    value_type width;
    value_type height;

    const static TRectSize<T>    ZERO;

    bool operator==(const TRectSize<T>& rh) const
    {
        return width == rh.width && height == rh.height;
    }

    bool operator!=(const TRectSize<T>& rh) const
    {
        return !operator==(rh);
    }
};

template<typename T>
const TRectSize<T> TRectSize<T>::ZERO = {0,0};


template<typename T>
struct TRect {
    typedef T value_type;

    value_type x;
    value_type y;
    value_type width;
    value_type height;

    const static TRect<T>    ZERO;

    void reset()
    {
        x = y = width = height = 0;
    }

    bool operator==(const TRect<T>& rh) const
    {
        return x == rh.x && y == rh.y && width == rh.width && height == rh.height;
    }
    bool operator!=(const TRect<T>& rh) const
    {
        return !operator==(rh);
    }

    bool isNull() const
    {
        return width == 0 || height == 0;
    }

    inline T getLeft() const
    {
        return x;
    }

    inline T getRight() const
    {
        return x + width;
    }

    inline T getWidth() const
    {
        return width;
    }

    inline T getTop() const
    {
        return y;
    }

    inline T getBottom() const
    {
        return y + height;
    }

    inline T getHeight() const
    {
        return height;
    }

    void joinWith(const TRect<T>& rh);
    void intersectWith(const TRect<T>& rh);

    bool isOverlap(const TRect<T>& rh) const;

    bool isValidSize() const
    {
        return 0 < width && 0 < height;
    }

    bool contains(const TRect<T>& rh) const;
    bool contains(T x, T y) const;
};

template<typename T>
const TRect<T> TRect<T>::ZERO = {0,0,0,0};


template<typename T>
void TRect<T>::joinWith(const TRect<T>& rh)
{
    *this = join(*this, rh);
}

template<typename T>
void TRect<T>::intersectWith(const TRect<T>& rh)
{
    *this = intersect(*this, rh);
}

template<typename T>
bool TRect<T>::isOverlap(const TRect<T>& rh) const
{
    return !(getRight() < rh.getLeft() || rh.getRight() < getLeft()
        || getBottom() < rh.getTop() || rh.getBottom() < getTop());
}

template<typename T>
bool TRect<T>::contains(const TRect<T>& rh) const
{
    return getLeft() <= rh.getLeft() && rh.getRight() <= getRight()
        && getTop() <= rh.getTop() && rh.getBottom() <= getBottom();
}

template<typename T>
bool TRect<T>::contains(T x, T y) const
{
    return getLeft() <= x && x <= getRight()
        && getTop() <= y && y <= getBottom();
}

template<typename T>
const TRect<T> join(const TRect<T>& lh, const TRect<T>& rh)
{
    if (lh.isNull())
        return rh;

    if (rh.isNull())
        return lh;

    T tx = lh.getLeft() < rh.getLeft() ? lh.getLeft() : rh.getLeft();
    T ty = lh.getTop() < rh.getTop() ? lh.getTop() : rh.getTop();
    T tr = lh.getRight() < rh.getRight() ? rh.getRight() : lh.getRight();
    T tb = lh.getBottom() < rh.getBottom() ? rh.getBottom() : lh.getBottom();

    TRect<T> r = initTRect(
        tx, ty,
        tr, tb);

    return r;
}

template<typename T>
const TRect<T> intersect(const TRect<T>& lh, const TRect<T>& rh)
{
    if (!lh.isOverlap(rh))
    {
        TRect<T> r = initTRect(0, 0, 0, 0);
        return r;
    }
    else
    {
        T tx = lh.getLeft() < rh.getLeft() ? rh.getLeft() : lh.getLeft();
        T ty = lh.getTop() < rh.getTop() ? rh.getTop() : lh.getTop();
        T tr = lh.getRight() < rh.getRight() ? lh.getRight() : rh.getRight();
        T tb = lh.getBottom() < rh.getBottom() ? lh.getBottom() : rh.getBottom();
        TRect<T> r = initTRect(
            tx, ty,
            tr, tb);
        return r;
    }
}

}


#endif
